package eg.com.itiga.ui.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import eg.com.itiga.business.exception.BusinessException;
import eg.com.itiga.business.factory.BusinessFactory;
import eg.com.itiga.domain.Itian;
import eg.com.itiga.domain.RegCompany;
import eg.com.itiga.ui.beans.UserProfile;

public class SuccessLogin extends org.apache.struts.actions.DispatchAction {
    
	final String itianProfile="ITIanProfile";
	final String companyProfile="companyProfile";
	final String adminProfile="admin";
	final String requestedParamter="id";
	    
	    public SuccessLogin() {
	    	
	    }
	    
	    public ActionForward getItian(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
	    	
	    	UserProfile u=((UserProfile)form);
	    	ActionForward act = new ActionForward();
	    	int id = (Integer)request.getAttribute(requestedParamter);
	    	try
	    	{
	    	act = mapping.findForward(itianProfile);
	    	
	    	
	    	Itian itian;
	    	
	    	itian = BusinessFactory.createItianService().retriveITIanData(id);
	    	if(itian != null){
	    	u.setEmail(itian.getIEmail());
	    	u.setDepartmentName(itian.getDepartment().getDName());
	    	u.setBranchName(itian.getBranch().getName());
	    	if(itian.getIYearsExp() != null)
	    	u.setYearsOfExperince(itian.getIYearsExp().toString());
	    	u.setPhone(itian.getIPhone());
	    	if(itian.getIIntake() != null)
	    	u.setIntackNo(itian.getIIntake().toString());
	    	u.setName(itian.getIName());
	    	}
	    	else
	    		System.out.println("this itian not exist");
	    
	    	}
	    	catch(BusinessException be)
	    	{
	    		be.printStackTrace();
	    		System.out.println("Exception in the business layer");
	    		
	    	}
	    	return act;
	       
	    }
	    public ActionForward getCompany(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
	        // TODO: Write method body
	       // throw new UnsupportedperationException("Method is not implemented");
	    	
	    	ActionForward act = new ActionForward();
	    	UserProfile u =((UserProfile)form);
	    	int id = (Integer)request.getAttribute(requestedParamter);
	    	try
	    	{
	  
	    	act=mapping.findForward(companyProfile);
	    	
	    	
	    	RegCompany com= new RegCompany();
	    	
	    	com = BusinessFactory.createCompanyService().retriveCompanyData(id);
	    	//Bean name
	    	u.setCompanyName(com.getCName());
	    	u.setCompanyWebsite(com.getRegWesite());
	    	u.setCompanyIsApproved(com.getRegIsApproved());
	    	if(com.getRegExpDate() != null)
	    	u.setCompanyExpireDate(com.getRegExpDate().toString());
	    	u.setCompanyState(com.getCState());
	    	//u.setCompanyContactDetails(com.getContactDetailses());
	    	//u.setCompanyFields(com.getCompanyFields());
	    	
	    	}
	    	catch(BusinessException be)
	    	{
	    		System.out.println("Exception in the business layer");
	    		
	    	}
	    	
	    	return act;
	       
	    }
	    public ActionForward getAdmin(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
	        // TODO: Write method body
	       // throw new UnsupportedperationException("Method is not implemented");
	    	
	    	ActionForward act = new ActionForward();
	    	
	    	act = mapping.findForward(adminProfile);
	    	
	    	return act;
	       
	    }
}